<template>
  <uni-nav-bar title="福利品" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package">
    <input class="uni-input" confirm-type="search" placeholder="搜索" @confirm="confirm" v-model="search"
      @input="interrogate" />
  </view>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <view class="package space-between" @click="navTo('administrator/welfare/shopDetail?id='+item.id)"
      v-for="(item,index) in frinedList" :key="item.id" :style="{marginTop:index==0?'0px':'15rpx',minHeight:'100rpx'}">
      <view style="width:85%;">{{item.name}}</view>
      <view>{{item.nums}}件</view>
    </view>
    <Popu v-if="frinedList.length<1" :pageHeight="pageHeight" text="暂无福利品列表"></Popu>
  </scroll-view>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { navBack, navTo } from '@/utils/navigator';
  import { showLoading, errorToast } from '@/utils/prompt';
  import Popu from '@c/earthPushing/common/popu.vue';
  import { getGoodsList } from '@/gql/warehouse';
  import { toPublish } from '@mqtt';
  /**
   * 福利品列表
   * @param search 搜索福利品信息
   * @param inquireAbout 是否在搜索状态
   * @param pageHeight 页面高度
   * @param frinedList 福利品列表
   * @param lastPage 总页数
   * @param currentPage 当前页数
   */
  const search = ref('')
  const inquireAbout = ref(false)

  const pageHeight = ref();
  const frinedList = ref([])
  const lastPage = ref(1);
  const currentPage = ref(1);
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 125;
    },
  });
  /**
   * 获取福利品信息
   * @param {Number} page  获取福利品列表数据
   * @param {String} keywords  搜索信息
   */
  function wareList() {
    showLoading()
    const payload = {
      query: getGoodsList,
      variables: {
        page: currentPage.value,
        keywords: search.value
      },
    };
    toPublish(
      'ql/control/getGoodsList',
      payload,
      (obj : any) => {
        const { getGoodsList } = obj.data;
        frinedList.value = [...frinedList.value, ...getGoodsList.list];
        lastPage.value = getGoodsList.lastpage;
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      wareList();
    }
  }
  /**
   * 搜索内容
   */
  function interrogate(evt : any) {
    const { detail } = evt;
    if (!detail.value && inquireAbout) {
      currentPage.value = 1
      frinedList.value = []
    }
  }
  /**
   * 搜索商品信息
   */
  function confirm() {
    if (search.value) {
      inquireAbout.value = search.value ? true : false;
      currentPage.value = 1
      frinedList.value = []
      wareList()
    } else {
      errorToast('请输入您要搜索的内容')
    }
  }
</script>

<style scoped lang="less">
  input {
    text-align: center;
  }
</style>